<?php
/**
 * Front page controller.
 *
 * @category Zend
 * @package Index
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class IndexController extends Zend_Controller_Action
{
    /**
     * @var int
     */
    protected $_categoryId;
    
    /**
     * Initialise properties
     */
    public function init()
    {
       $this->_page = $this->_request->getParam('page', 1);
       $this->_categoryId = $this->_request->categoryId;
       $this->view->category = $this->_request->category;
    }
    
    /**
     * Index action
     */
    public function indexAction()
    {
        $form = new Application_Form_Post();
        $form->populate(array('category' => $this->_categoryId));
        $post = $this->_request->getPost();
        $active = $this->_request->getParam('active', 'top');
        
        if ($this->_request->isPost() && $form->isValid($post)) {
            $this->_createDispute($form);
            $this->_helper->FlashMessenger->addMessage('newPost');
            $this->_redirect('/');
        } elseif ($this->_request->isPost()) {
            $active = 'post';
        }
        
        if ($this->_helper->flash('newPost')) {
            $this->view->post = true;
            $active = 'new';
        }
        
        $this->view->form = $form;
        $this->view->active = $active;
        $this->view->signup = $this->_helper->flash('newSignup');
        $this->view->top = Disputeo_Model_DisputeMapper::getTop($this->_categoryId, $this->_page);
        $this->view->new = Disputeo_Model_DisputeMapper::getNew($this->_categoryId, $this->_page);
        $this->view->open = Disputeo_Model_DisputeMapper::getOpen($this->_categoryId, $this->_page);
    }
    
    /**
     * Create a new dispute post
     * 
     * @param Application_Form_Post $form 
     */
    protected function _createDispute(Application_Form_Post $form)
    {
        Application_Model_Dispute::create(array(
            'userId' => $this->_request->session->userId,
            'categoryId' => $form->postcat->getValue(),
            'title' => $form->title->getValue(),
            'argument' => $form->argument->getValue()
        ));
    }
    
    /**
     * Create a dispute response
     * 
     * @param Application_Form_Response $form
     */
    protected function _createResponse(Application_Form_Response $form)
    {
        Application_Model_Response::create(array(
            'disputeId' => $form->disputeId->getValue(),
            'title' => $form->title->getValue(),
            'response' => $form->response->getValue(),
            'userId' => $this->_request->session->userId
        ));
    }
    
    /**
     * 
     */
    public function disputeAction()
    {
        $post = $this->_request->getPost();
        $dispute = $this->_request->dispute;
        $form = new Application_Form_Response(array(
            'disputeId' => $dispute->disputeId
        ));
        
        if ($this->_request->isPost() && $form->isValid($post)) {
            $this->_createResponse($form);
            $this->_redirect('/disputeo/dispute/' . $dispute->disputeId);
        }
        
        $this->view->dispute = $dispute;
        $this->view->form = $form;
        $this->view->active = $form->isErrors() ? 'contest' : 'argument';
    }
    
    /**
     * Dispute up vote
     */
    public function xhrUpVoteAction()
    {
        $this->_addVote(1);
    }
    
    /**
     * Dispute down vote
     */
    public function xhrDownVoteAction()
    {
        $this->_addVote(0);
    }
    
    /**
     * Add a vote for this user
     * 
     * @param int $vote
     */
    protected function _addVote($vote)
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->_request->session->userId && $this->_request->dispute) {
            Application_Model_Vote::create(array(
                'disputeId' => $this->_request->dispute->disputeId,
                'userId' => $this->_request->session->userId,
                'vote' => $vote
            ));
        }
    }
}

